package com.gklyj.controller.qiuzhi;

import com.gklyj.pojo.Application;
import com.gklyj.pojo.Position;
import com.gklyj.pojo.User;
import com.gklyj.service.applicationService;
import com.gklyj.service.positionService;
import com.gklyj.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@Transactional
public class Qz_gwsqController {
    @Autowired
    positionService positionService;

    @Autowired
    applicationService applicationService;

    @Autowired
    userService userService;

    /*
    职位列表
     */
    @GetMapping("/ListPositions")
    @ResponseBody
    public List<Position> ListPositions(){
        return positionService.list();
    }
    /*
    信息查询
     */
    @PostMapping("/gwsq_search")
    @ResponseBody
    public List<Position> SearchPositions(@RequestBody Position position){
        return positionService.searchPositions(position);
    }
    /*
    详情页面
     */
    @GetMapping("/toDetail")
    public String toDetail(@RequestParam("id") String id, Model model){
        Position position = positionService.getById(Integer.valueOf(id));
        model.addAttribute("detail",position);
        return "qiuzhi/qz-gwsq-detail";
    }

    /*
    点击申请后，弹出备注填写
     */
    @GetMapping("/toApply")
    public String toApply(@RequestParam("id") String id, Model model){
        Position position = positionService.getById(Integer.valueOf(id));
        model.addAttribute("detail",position);
        return "qiuzhi/qz-gwsq-apply";
    }

    /*
    岗位申请
     */
    @PostMapping("/applyGw")
    @ResponseBody
    public Integer ApplyPosition(@RequestBody Application application, HttpServletRequest request) throws RuntimeException{

        System.out.println("9999999999999999"+application);
        String dname = application.getName();

        Object username = request.getSession().getAttribute("username");
        User user = userService.findUserByUsername(String.valueOf(username));
        application.setU_id(user.getU_id());
        application.setName(user.getName());
        application.setState("审核中");
        //获取当前年月日
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示时分秒
        Timestamp now= new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String today= df.format(now);
        application.setTime(today);

        //查询该条申请记录
        Position position = positionService.FindOnePosition1(application.getDepartment(), application.getP_title(),dname);
        if(position.getLim()<=position.getQuantity()){
            return 3;      //4表示招聘人数已满
        }
        //判断岗位是否申请过,,0表示保存失败，1表示保存成功，2表示重复申请
        if(applicationService.SearchOneApplication(application)!=null){
            return 2;  //2表示重复申请
        }else {
            boolean sta = applicationService.save(application);
            if (sta){
                return 1;        //1表示保存成功
            }else {
                return 0;          //0表示保存失败
            }
        }
    }
}
